package cn.kgc.shiro.config;

import org.apache.shiro.authc.*;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;

/**
 * @author 课工场
 * @date 2024/6/1
 * @description   用户自定义域对象
 */
public class CustomerRealm extends AuthorizingRealm {


    //  授权  用户访问的接口是受限资源  则会自动调用该方法
    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
        return null;
    }

    //  认证方法  只根据用户名查询用户信息 查询到结果直接封装成 AuthenticationInfo 在该方法中不做密码校验
    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {

        System.out.println("CustomerRealm >> doGetAuthenticationInfo");
        UsernamePasswordToken usernamePasswordToken = (UsernamePasswordToken) token;
        String username = usernamePasswordToken.getUsername();
        System.out.println("username = " + username);

        // TODO 根据用户名 查询数据库     username=admin   password= sdgdahsjh1241
        if ("admin".equals(username)){   // user
            SimpleAuthenticationInfo simpleAuthenticationInfo = new SimpleAuthenticationInfo("admin","sdgdahsjh1241",this.getName());
            return simpleAuthenticationInfo;
        }

        return null;
    }
}
